/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulation;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.AMSAgentDescription;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.SearchConstraints;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;

/**
 *
 * @author Danielondon
 */
public class ServiceDirectory {
    private static String SERVICE_NAME = "PEV Simulation";
    
    public static boolean registerAgentService(Agent agent, ServiceSimulationDescription service) {
        
        // Adds Service
        // Register the simulation service in the yellow pages
        // with the purpose to recieve messages of the simulation controller
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(agent.getAID());
        ServiceDescription sd = new ServiceDescription();
        sd.setType(service.toString());
        sd.setName(SERVICE_NAME);
        dfd.addServices(sd);
        try {
            DFService.register(agent, dfd);            
            return true;
        }
        catch (FIPAException fe) {
            fe.printStackTrace();
        }
        
        return false;
    }
    
    public static AID[] getProvidersFromService(Agent agent, ServiceSimulationDescription serviceDescription) {       
        AID [] providers = null;
        DFAgentDescription template = new DFAgentDescription();
        ServiceDescription sd = new ServiceDescription();
        sd.setType(serviceDescription.toString());
        sd.setName(SERVICE_NAME);
        template.addServices(sd);
        try {
            //SearchConstraints constraints = new SearchConstraints();
            //constraints.setMaxResults((long)500);
            //constraints.setMaxDepth((long)500);
            DFAgentDescription[] result = DFService.search(agent, template);//, constraints );             
            providers = new AID[result.length];
            for (int i = 0; i < result.length; ++i) {
                providers[i] = result[i].getName();
            }
        }
        catch (FIPAException fe) {
            fe.printStackTrace();
        }
        
        return providers;
    }
}
